package com.example.commonlib.httplib.utils;

import com.google.gson.GsonBuilder;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Create by xjs
 * _______date : 17/8/3
 * _______description:
 */
public class ASCIISortUtil {
    /**
     * 将参数按照字段名的 ASCII 码从小到大排序（字典序）后，使用 URL 键值
     * 对的格式（即key1=value1&key2=value2…）拼接成字符串
     *
     * @param paraMap     转换参数
     * @param isURLEncode 是否对所有Value进行URLEncod转码
     * @return
     */
    public static String formatQueryParaMap(Map<String, String> paraMap, boolean isURLEncode) {
        String returnValue;
        try {
            List<Map.Entry<String, String>> infoIds = new ArrayList<>(paraMap.entrySet());
            Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
                @Override
                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return (o1.getKey()).compareTo(o2.getKey());
                }
            });
            StringBuffer buff = new StringBuffer();
            for (int i = 0; i < infoIds.size(); i++) {
                Map.Entry<String, String> item = infoIds.get(i);
                if (null != item.getKey() && !"".equals(item.getKey())) {
                    String key = item.getKey();
                    Object val = item.getValue();
                    String json = new GsonBuilder().create().toJson(val);
                    if (isURLEncode) {
                        val = URLEncoder.encode(val.toString(), "UTF-8");
                        /* 空格通过URLEncoder转码后是“+”,所有要替换成“%20” */
                        val = val.toString().replace("+", "%20");
                    }
                    if (json.startsWith("\"")) {
                        buff.append(key).append("=").append(val).append("&");
                    }
                }
            }
            returnValue = buff.toString();
            if (null != returnValue && !"".equals(returnValue)) {
                returnValue = returnValue.substring(0, returnValue.length() - 1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return returnValue;
    }
}
